package com.element.security.exception;

import com.common.core.exception.AbstractException;
import com.common.core.exception.code.SecurityErrorCode;
import com.common.core.results.VoidResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.oauth2.common.exceptions.*;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpMediaTypeNotSupportedException;

/**
 * @auther zhangwj
 * @date 2021/3/24 下午5:11
 */
@Component
public class AuthResponseException implements WebResponseExceptionTranslator<OAuth2Exception> {

    @Override
    public ResponseEntity<OAuth2Exception> translate(Exception e) {
        VoidResult result = VoidResult.failed(SecurityErrorCode.SYS_EXCEPTION_CODE);
        if (e instanceof InvalidGrantException) {
            String msg = e.getMessage();
            if (null != msg && msg.contains("用户名或密码错误")) {
                result = VoidResult.failed(SecurityErrorCode.A_USERNAME_OR_PASSWORD_ERROR);
            } else {
                result = VoidResult.failed(SecurityErrorCode.A_INVALID_GRANT);
            }
        } else if (e instanceof UnsupportedGrantTypeException) {
            result = VoidResult.failed(SecurityErrorCode.A_UNSUPPORTED_GRANT_TYPE);
        } else if (e instanceof InvalidRequestException) {
            result = VoidResult.failed(SecurityErrorCode.A_INVALID_REQUEST);
        } else if (e instanceof InvalidScopeException) {
            result = VoidResult.failed(SecurityErrorCode.A_INVALID_SCOPE);
        } else if (e instanceof HttpMediaTypeNotSupportedException) {
            result = VoidResult.failed(SecurityErrorCode.A_HTTP_MEDIA_TYPE_NOT_SUPPORTED);
        } else if (e instanceof InvalidClientException) {
            result = VoidResult.failed(SecurityErrorCode.A_INVALID_CLIENT_EXCEPTION);
            // String msg = e.getMessage();
            // result.setErrMsg(msg);
        } else if (e instanceof UsernameNotFoundException) {
            result = VoidResult.failed(SecurityErrorCode.A_USERNAME_NOT_FOUND_EXCEPTION);
        } else if (e instanceof AbstractException) {
            AbstractException abstractException = (AbstractException) e;
            result.setErrNum(Integer.parseInt(abstractException.getErrCode()));
            result.setErrMsg(abstractException.getMessage());
        } else if (e instanceof AuthException) {
            AuthException authException = (AuthException) e;
            result.setErrMsg(authException.getResult().getErrMsg());
        } else if (e instanceof InvalidTokenException) {
            result = VoidResult.failed(SecurityErrorCode.A_INVALID_TOKEN_EXCEPTION);
        } else if (e instanceof GlobalAuthenticationException) {
            result = VoidResult.failed(SecurityErrorCode.SYS_EXCEPTION_CODE);
            result.setErrMsg(e.getMessage());
        } else {
            result = VoidResult.failed(SecurityErrorCode.SYS_EXCEPTION_CODE);
            result.setErrMsg("鉴权异常");
        }
        return new ResponseEntity<>(AuthException.getInstance(result), HttpStatus.valueOf(HttpStatus.OK.value()));
    }
}
